package com.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dao.UserinfoDao;
import com.enetiy.Userimg;
import com.enetiy.Userinfo;
import com.enetiy.Users;

public class UserinfoDaoImpl extends HibernateDaoSupport implements UserinfoDao {

	@Override
	public Userinfo selectUserinfo(int uid) throws Exception {
		Query query = this.getSession().createQuery("from Userinfo where users_id=?");
		query.setParameter(0, uid);
		List<Userinfo> list = query.list();
		return list.isEmpty()?null:list.get(0);
	}

	@Override
	public void updateUserinfo(Userinfo ui) throws Exception {
		this.getHibernateTemplate().update(ui);
      
	}

	@Override
	public Serializable insertUserinfo(Userinfo ui) throws Exception {
		
		return this.getHibernateTemplate().save(ui);
	}

	@Override
	public void deleteUserinfo(Userinfo ui) throws Exception {
		this.getHibernateTemplate().delete(ui);
		
	}

	@Override
	public Userinfo selectUserToken(String token, int userid) throws Exception {
		Query query = this.getSession().createQuery("from Userinfo where token=? and users_id=?");
		query.setParameter(0, token);
		query.setParameter(1, userid);
		List <Userinfo> list = query.list();	
		return list.size()>0?list.get(0):null;
	}

	@Override
	public Userinfo selectUserMac(String Mac) throws Exception {
		Query query = this.getSession().createQuery("from Userinfo where user_mac=?");
		query.setParameter(0, Mac);
		List <Userinfo> list = query.list();	
		return list.size()>0?list.get(0):null;
	}

	@Override
	public List<Userinfo> selectUserinfos() throws Exception {
		List<Userinfo> find = this.getHibernateTemplate().find("from Userinfo");
		return find;
	}


}
